package com.boang.controller.parking;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boang.entity.PO.Guardbooth;
import com.boang.resp.Result;
import com.boang.service.GuardboothService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "岗亭管理", description = "岗亭相关接口")
@RestController
@RequestMapping("/guardbooth")
public class GuardboothController {

    @Autowired
    private GuardboothService guardboothService;

    //------------------- 添加 --------------------
    @Operation(summary = "添加岗亭")
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody Guardbooth guardbooth) {
        return Result.ok(guardboothService.save(guardbooth));
    }

    //------------------- 修改 --------------------
    @Operation(summary = "修改岗亭信息")
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody Guardbooth guardbooth) {
        return Result.ok(guardboothService.updateById(guardbooth));
    }

    //------------------- 删除 --------------------
    @Operation(summary = "删除岗亭")
    @DeleteMapping("/remove")
    public Result<Boolean> remove(Integer id) {
        return Result.ok(guardboothService.removeById(id));
    }

    //------------------- 查询 --------------------
    @Operation(summary = "条件分页查询岗亭")
    @PostMapping("/page")
    public IPage<Guardbooth> page(@RequestBody Guardbooth guardbooth) {
        QueryWrapper<Guardbooth> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(guardbooth.getBoothNumber() != null, Guardbooth::getBoothNumber, guardbooth.getBoothNumber())
                .like(guardbooth.getBoothName() != null, Guardbooth::getBoothName, guardbooth.getBoothName())
//                .eq(guardbooth.getParkingLot() != null, Guardbooth::getParkingLot, guardbooth.getParkingLot())
//                .eq(guardbooth.getTempCarEntry() != 0, Guardbooth::getTempCarEntry, guardbooth.getTempCarEntry())
//                .eq(guardbooth.getTempCarApproval() != 0, Guardbooth::getTempCarApproval, guardbooth.getTempCarApproval())
                .eq(guardbooth.getIsCharging() != 0, Guardbooth::getIsCharging, guardbooth.getIsCharging());

        // 处理分页参数
        int pageNum = guardbooth.getPageNum() < 1 ? 1 : guardbooth.getPageNum();
        int pageSize = (guardbooth.getPageSize() < 1 || guardbooth.getPageSize() > 100) ? 10 : guardbooth.getPageSize();
        
        Page<Guardbooth> page = new Page<>(pageNum, pageSize);
        return guardboothService.page(page, queryWrapper);
    }
    //------------------- 查询所有岗亭 --------------------
    @Operation(summary = "查询所有岗亭")
    @GetMapping("/list")
    public Result<List<Guardbooth>> list() {
        return Result.ok(guardboothService.list());
    }
}